﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using VietStar.Library.Entity;
using VietStar.Library.Commom.SqlServer;

namespace VietStar.Library.Data
{
    public class ProductData
    {
        public ProductData() { }
        Connection cn = new Connection();
        ProductEntity entity = new ProductEntity();

        public bool Insert(ProductEntity entity)
        {
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("SanPham_Insert", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@NhaSanXuat", entity.NhaSanXuat));
                    cmd.Parameters.Add(new SqlParameter("@LoaiSanPham", entity.LoaiSanPham));
                    cmd.Parameters.Add(new SqlParameter("@TenSanPham", entity.TenSanPham));
                    cmd.Parameters.Add(new SqlParameter("@DonGia", entity.DonGia));
                    cmd.Parameters.Add(new SqlParameter("@XuatXu", entity.XuatXu));
                    cmd.Parameters.Add(new SqlParameter("@Xem", entity.Xem));
                    cmd.Parameters.Add(new SqlParameter("@KhuyenMai", entity.KhuyenMai));
                    cmd.Parameters.Add(new SqlParameter("@Hinh", entity.Hinh));
                    cmd.Parameters.Add(new SqlParameter("@TrangThai", entity.TrangThai));
                    cmd.Parameters.Add(new SqlParameter("@MoTa", entity.MoTa));
                    cmd.ExecuteNonQuery();
                    //done
                    connection.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Update(ProductEntity entity)
        {
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("SanPham_Update", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@NhaSanXuat", entity.NhaSanXuat));
                    cmd.Parameters.Add(new SqlParameter("@LoaiSanPham", entity.LoaiSanPham));
                    cmd.Parameters.Add(new SqlParameter("@TenSanPham", entity.TenSanPham));
                    cmd.Parameters.Add(new SqlParameter("@DonGia", entity.DonGia));
                    cmd.Parameters.Add(new SqlParameter("@XuatXu", entity.XuatXu));
                    cmd.Parameters.Add(new SqlParameter("@Xem", entity.Xem));
                    cmd.Parameters.Add(new SqlParameter("@KhuyenMai", entity.KhuyenMai));
                    cmd.Parameters.Add(new SqlParameter("@Hinh", entity.Hinh));
                    cmd.Parameters.Add(new SqlParameter("@TrangThai", entity.TrangThai));
                    cmd.Parameters.Add(new SqlParameter("@MoTa", entity.MoTa));
                    cmd.ExecuteNonQuery();
                    //done
                    connection.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Delete(int Id)
        {
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("SanPham_Delete", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Id", Id));
                    cmd.ExecuteNonQuery();
                    //done
                    connection.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public List<ProductEntity> GetAll()
        {
            List<ProductEntity> list = new List<ProductEntity>();
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("SanPham_All", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            list.Add(EntityFromReader(reader));
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
            return list;
        }
        public List<ProductEntity> GetById(int Id)
        {
            List<ProductEntity> list = new List<ProductEntity>();
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("SanPham_ById", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Id", Id));
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            list.Add(EntityFromReader(reader));
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
            return list;
        }
        private ProductEntity EntityFromReader(IDataReader reader)
        {
            ProductEntity entity = new ProductEntity();
            if (!reader.IsClosed)
            {
                if (reader["Id"] != null)
                    entity.Id = (int)reader["Id"];
                if (reader["NhaSanXuat"] != null)
                    entity.NhaSanXuat = (int)reader["NhaSanXuat"];
                if (reader["LoaiSanPham"] != null)
                    entity.LoaiSanPham = (int)reader["LoaiSanPham"];
                if (reader["TenSanPham"] != null)
                    entity.TenSanPham = (string)reader["TenSanPham"];
                if (reader["DonGia"] != null)
                    entity.DonGia = (double)reader["DonGia"];
                if (reader["XuatXu"] != null)
                    entity.XuatXu = (string)reader["XuatXu"];
                if (reader["Xem"] != null)
                    entity.Xem = (int)reader["Xem"];
                if (reader["KhuyenMai"] != null)
                    entity.KhuyenMai = (bool)reader["KhuyenMai"];
                if (reader["Hinh"] != null)
                    entity.Hinh = (string)reader["Hinh"];
                if (reader["TrangThai"] != null)
                    entity.TrangThai = (bool)reader["TrangThai"];
                if (reader["MoTa"] != null)
                    entity.MoTa = (string)reader["MoTa"];
            }
            return entity;
        }





    }
}
